package com.tledu.zyf.controller;

import java.io.UnsupportedEncodingException;
import java.util.Base64;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.tledu.zyf.manager.DataProcessManager;
import com.tledu.zyf.model.IPAndLocationPojo;
import com.tledu.zyf.service.IIpService;

@Controller
public class IPController {

	@Autowired
	private IIpService ipService;

	@RequestMapping(value = "/select", produces = { "text/html;charset=utf-8" })
	@ResponseBody
	public String list(@RequestParam String ip)
			throws UnsupportedEncodingException {
		System.out.println(ip);
		// IP不写死
		if (ip == null) {
			return "传入的参数个数不对,请传入要查询的IP";
		}

		List<IPAndLocationPojo> iPAndLocationPojos = ipService.list();
		DataProcessManager.ipAndLocationPojoArray = DataProcessManager
				.convertListToArrayAndSort(iPAndLocationPojos);
		String location = DataProcessManager.getLocation(ip);
		location = new String(Base64.getDecoder().decode(location), "utf-8");
		System.out.println(new String(Base64.getDecoder().decode(location),
				"utf-8") + "--");

		return new String(Base64.getDecoder().decode(location), "utf-8");
	}

	/**
	 * 设置项目启动自己寻找的页面
	 * 
	 * @return
	 */
	@RequestMapping("/")
	public String index() {
		return "index";
	}
}